package com.yswl.yeeduo.ui.task;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.yswl.yeeduo.App;
import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.asynctask.BaseHttpTask;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.http.HttpUtils;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.ui.tool.UrlParamsConfig;

/**
 * 提现
 * 
 * @author Administrator
 * 
 */
public class DrawMoneyTask extends BaseHttpTask<Map<String, String>, Void, Object> {

	public DrawMoneyTask(Context context, int taskId, TaskDelegate delegate) {
		super(context, taskId, delegate);

	}

	/**
	 * 首先执行该方法，在真正的后台操作前北UI线程调用，可在该方法内执行一些准备工作，如：进度条或控件的实例化
	 */
	protected void onPreExecute() {
		super.onPreExecute();
		super.startProgressDialog();
	}

	@Override
	protected Object doInBackground(Map<String, String>... params) {
		String str = "";
		String url = UrlParamsConfig.getURLEncapsulation(Constant.ConfigNet.user_drawmoney, params[0], ServiceAuthManager.getSecret(context));
		try {
			str = HttpUtils.sendGetMethod4Path(url, new HashMap<String, Object>(), "utf-8");
		} catch (ClientProtocolException e) {
			return new NetworkException();
		} catch (IOException e) {
			return new NetworkException();
		}

		return str;
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		stopProgressDialog();

		if (result instanceof NetworkException) {
			Toast.makeText(App.getInstance(), "网络异常", Toast.LENGTH_SHORT).show();
			return;
		}

		JSONObject json = null;
		try {
			json = new JSONObject(result + "");
			if (json.getInt(RETURN_CODE) != 0) {
				if (json.getInt(RETURN_CODE) == 11) {
					ToastUtil.showShortMsg("未绑定微信");
					return;
				}
				String msg = getMSG(json);
				if (TextUtils.isEmpty(msg) || "null".equals(msg)) {
					msg = "异常操作";
				}
				this.getDelegate().faild_TaskDelegate(super.getTaskId(), msg);
			} else {

				JSONObject jsonObj = json.getJSONObject(DATA);
				String active = jsonObj.getString("active");
				String msg = jsonObj.getString("message");
				if (active.equalsIgnoreCase("SUCCESS")) {
					this.getDelegate().success_TaskDelegate(super.getTaskId(), msg);//
				} else if (active.equalsIgnoreCase("FAIL")) {
					this.getDelegate().faild_TaskDelegate(super.getTaskId(), msg);
				}
			}
		} catch (JSONException e) {
			Toast.makeText(App.getInstance(), "发现异常", Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * 用户取消线程时调用
	 */
	protected void onCancelled() {
		super.onCancelled();
		stopProgressDialog();
	}

}